package com.putprize

import org.eclipse.jetty.server.Server
import org.eclipse.jetty.servlet.{ServletContextHandler,ServletHolder}

import com.putprize.percy.{Count,Measure,Scorer}
import com.putprize.percy.service.{PyotrService,PyotrServlet,PercyInferServlet}

import org.apache.log4j.Logger

object PyotrServer {
  
  val _log = Logger.getLogger(this.getClass.getName)
  
  def run(LOCATION_COUNT_T:String, LOCATION_COUNT_V:String, LOCATION_MEASURE:String, 
      DOC_NUM:Int, WEIGHT:Boolean, MIN_VALUE_SCORE:Double,port:Int) = {
    
    _log.info("Init Count ...")
    val cT = Count.initCountT(LOCATION_COUNT_T)
    val cV = Count.initCountV(LOCATION_COUNT_V)
    _log.info("Init Count Done")
    
    _log.info("Get Measure ...")
    val m = Measure.getData(LOCATION_MEASURE)
    _log.info("Get Measure Done "+m.size)
    val mS = new Scorer(m,DOC_NUM)
    
    _log.info("Service Init ...")
    val service = new PyotrService(cT,cV, mS, MIN_VALUE_SCORE)
    _log.info("Service Init Done")
    
    val server = new Server(port)
    
    val root = new ServletContextHandler(server,"")
    val s = new PyotrServlet(service,WEIGHT)
    root.addServlet(new ServletHolder(s),"/test")
    
    val inferServlet = new PercyInferServlet(service,WEIGHT)
    root.addServlet(new ServletHolder(s),"/infer")
    
    try {
      server.start
    }
    catch {
      case e:Exception => {
        e.printStackTrace
        sys.exit(1)
      }
    }
    
  }
  
}
